package cn.javaxc.model.base;
import cn.javaxc.mvcwork.controller.web.*;
import cn.javaxc.model.page.VUEFormBean;
import cn.javaxc.mvcwork.util.common.CommonUtil;
import cn.javaxc.mvcwork.util.common.DatabaseUtils;
import java.sql.*;
import java.util.*;

/**
 * 框架Sql执行后的元数据<br/>
 * 进行对元数据的增删改查<br/>
 *
 *
 */

public class SQLMap extends LinkedHashMap{

  protected String tableName = null;
  protected Object code = "";
  protected String description = "";
  protected boolean defaultItem = false;
  protected boolean enabled = true;
  protected boolean group = false;
//	protected Map propertiesMap=new HashMap();
	protected Map<String,String> namesMap=new HashMap<String,String>();
//	protected ParamType typesMap;
	protected TreeOutline treeData;
	protected boolean isLog=true;
	public SQLMap(){
//		typesMap=new ParamType();
		namesMap.put("code","code");
		namesMap.put("description","description");
	}
    protected SQLList createList(){
		return new SQLList();
    }	
	public boolean isLog(){
		return this.isLog;
	}
	public void setIsLog(boolean isLog){
		this.isLog=isLog;
	}
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>查询返回的的数据库数据的结果
	 */
	public Map getMap(){
		return this;
	}
	public String getMapString(){
		return super.toString();
	}
//	public Object getMap(Object key){
//		return LinkedHashMap.this.get(key);
//	}
//	public void setMap(Map map){
//		if(map!=null)putAll(map);
//	}
	
	public Map getNamesMap(){
		return this.namesMap;
	}
	public void setNamesMap(Map namesMap){
		this.namesMap=namesMap;
	}



  /**
   *  Sets the tableName attribute of the SQLMap object
   *
   * @param  tmp  The new tableName value
   */
  public void setTableName(String tmp) {
    this.tableName = tmp;
  }
	public void setTable(String tmp) {
    this.tableName = tmp;
  }






  /**
   *  Sets the code attribute of the SQLMap object
   *
   * @param  tmp  The new code value
   */
  public void setCode(Object tmp) {
    this.code = tmp;
	put("__code",tmp);
  }




  /**
   *  Sets the Description attribute of the SQLMap object
   *
   * @param  tmp  The new Description value
   * @since       1.1
   */
  public void setDescription(String tmp) {
    this.description = tmp;
	put("__desc",tmp);
  }



  /**
   *  Gets the tableName attribute of the SQLMap object
   *
   * @return    The tableName value
   */
  public String getTableName() {
    return tableName;
  }


  /**
   *  获取元数据的id列值
   *
   * @return    The Code value
   * @since     1.1
   */
  public Object getCode() {
    return code;
  }





  /**
   *  获取元数据的名称列值
   *
   * @return    The Description value
   * @since     1.1
   */
  public String getDescription() {
    return description;
  }



  /**
   *  Description of the Method
   *
   * @param  rs                      Description of the Parameter
   * @throws  java.sql.SQLException  Description of the Exception
   */
  public void build(Map map) {
	 putAll(map);
//		
	if(this.containsKey(namesMap.get("code"))){
		if (this.get(namesMap.get("code"))!=null){
			code = this.get(namesMap.get("code"));
		}
	}
//    
	if(this.containsKey(namesMap.get("description"))){
		if (this.get(namesMap.get("description"))!=null){
			description = this.get(namesMap.get("description"))+"";
		}
	}
			
  }




	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>增加元数据
	 * @return 返回增加产生的id
	 */
	public int insert(String sql) {
		return insert(sql,this);
	}
  public int insert(String sql,Map data)  {
		return insert(sql,data,null,null);
  }
  public int insert(String sql,Map data,String cntSql,VUEFormBean emtMap) {
//	  if ("".equals(sql))throw new ISQLException("insert sql为空");
//	      PreparedStatement pst = null;
//		int i = 0;
//		try{
//		pst = db.prepareStatement(sql);
//		pst.execute();
//		//System.setProperty("debugsql","false");
//		pst.close();
//		i = DatabaseUtils.getCurrVal(db,"",-1);
//		}catch(SQLException e){
//			throw new SQLException(e.getMessage()+" "+sql);
//		}
//		code=i+"";
		return 0;

  }
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>更新元数据
	 * @return 返回更新的个数
	 */ 
	public int update(String sql) {
		return update(sql,this);
	}
	public int update(String sql,Map data) {
		return update(sql,data,null,null);
	}
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>更新元数据
	 * @return 返回更新的个数
	 */ 
	public int update(String sql,Map data,String cntSql,VUEFormBean emtMap) {
//	 if ("".equals(sql)){
//		throw new ISQLException("update sql为空");
//	}
//    int resultCount = 0;
//	try{
//		
//		PreparedStatement pst = db.prepareStatement(sql);
//		resultCount = pst.executeUpdate();
//		pst.close();
//	}catch(SQLException e){
//		throw new SQLException(e.getMessage()+" "+sql);
//	}
    
    return 0;
	 
	 }



	

	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>删除元数据
	 */
	public boolean execute(String sql) {
		return execute(sql,this);
	}
	public boolean execute(String sql,Map data) {
		return execute(sql,data,null,null);
	}
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>删除元数据
	 */
	public boolean execute(String sql,Map data,String cntSql,VUEFormBean emtMap) {
//		if ("".equals(sql)){
//			throw new ISQLException("delete sql为空");
//		}
//		boolean ret=false;
//		try{
//		
//			PreparedStatement pst = db.prepareStatement(sql);
//			ret=pst.execute();
//			pst.close();
//		}catch(SQLException e){
//			throw new SQLException(e.getMessage()+" "+sql);
//		}
		
		return true;
	}
	public SQLList executeQuery(String sql,String cntSql)  {
		SQLList elist=createList();
//		elist.setTypesMap(typesMap);
		elist.buildList(sql, cntSql, this);
		return elist;
	}
	/**
	 * <font color='red' size='4' style='font-weight:900'>*</font>批量更新元数据
	 * @param  table  元数据批量操作时对应的表<br>
	 *				  一次Action调度,可有多个批量操作,使用表对各自的批量操作进行标识
	 * @return 返回批量操作的个数
	 */ 
	public int batch(String sql,String table,VUEFormBean emtMap,String cntSql) {
		
//		if ("".equals(table)){
//				throw new ISQLException("updateInsert 表名为空");
//		}
		int ret=0;
		for (SQLMap _emt:emtMap.getElements().values()){
			if (table.equalsIgnoreCase("all")||table.equalsIgnoreCase(_emt.getTableName())){
				execute(sql,_emt,cntSql,emtMap);
				ret++;
			}
		}
		return ret;
	 }

  /**
   *  Description of the Method
   *
   * @return    Description of the Return Value
   */
  public String toString() {
    return "name:" + description+",id:"+code+",table:"+tableName;
  }


  public void setName(String key,String value){
	namesMap.put(key,value);
  }
  public void setProperty(Object key,Object value){
	this.put(key,value);
  }
	/**
	 * 同getPropertiesMap().get(key)
	 * @see #getMap()
	 */
	public Object get(Object key){
		if ("map".equals(key)){
			return this;
		}else if ("__map".equals(key)){
			return getMapString();
		}else if ("__code".equals(key)){
			return getCode();
		}else if ("__desc".equals(key)){
			return getDescription();
		}else if ("__tree".equals(key)){
			return getTreeOutline();
		}else if(getMap(key+"_desc")!=null){
			return getMap(key+"_desc");
		}
		return getMap(key);
	}
	public Object getMap(Object clmn){
		return super.get(clmn);
	}
	/**
	 * 将数据库数据转化为时间类型
	 * @param clmn propertiesMap中的列名
	 * @return 若为无效时间,则返回null
	 */
	public Timestamp getTimestamp(String clmn){
		if (this.get(clmn) instanceof Timestamp){
			return (Timestamp)this.get(clmn);
		}
		return null;
	}
	/**
	 * 将数据库数据转化为Int
	 * @param clmn propertiesMap中的列名
	 * @return 若为无效数字,则返回-1
	 */
	public int getInt(String clmn){
		return CommonUtil.parseInt(this.get(clmn),-1);
	}
	public int getInt(String clmn,int _def){
		return CommonUtil.parseInt(this.get(clmn),_def);
	}
	/**
	 * 将数据库数据转化为String
	 * @param clmn propertiesMap中的列名
	 * @return 若为无效数据,则返回空
	 */
	public String getString(String clmn){
		Object o=this.get(clmn);
		if (o!=null){
			return o.toString();
		}
		return "";
	}
	public long getLong(String clmn){
		return CommonUtil.parseLong(this.get(clmn),0);
	}
	public double getDouble(String clmn){
		return CommonUtil.parseDouble(this.get(clmn),0);
	}
	public boolean getBoolean(String clmn){
		return DatabaseUtils.parseBoolean(String.valueOf(this.get(clmn)));
	}
	public void setObject(Object key,Object value){
		setProperty(key,value);
	}
	public void setInt(Object key,int value){
		setProperty(key,String.valueOf(value));
	}
	public void setString(Object key,String value){
		setProperty(key,value);
	}
	public void setDouble(Object key,double value){
		setProperty(key,String.valueOf(value));
	}
	public void setBoolean(Object key,boolean value){
		setProperty(key,String.valueOf(value?1:0));
	}
	public void setTimestamp(Object key,Timestamp value){
		setProperty(key,String.valueOf(value));
	}

//	/**
//	 * 同getPropertiesMap
//	 * @see #getMap()
//	 */
//	public Map getMap(){
//		return getMap();
//	}
//	public void setMap(Map m){
//		setMap(m);
//	}

	public TreeOutline getTreeData(){
		return getTreeOutline();
	}
	public TreeOutline getTreeOutline(){
		return this.treeData;
	}
	public void setTreeOutline(TreeOutline treeData){
		this.treeData=treeData;
	}
}
